<?php
    /**
     * @license       http://opensource.org/licenses/Apache-2.0 Apache 2.0
     */

    App::uses( 'AppController', 'Controller' );

    /**
     * Static content controller
     *
     * Override this controller by placing a copy in controllers directory of an application
     *
     * @package       app.Contaroller
     * @link          http://book.cakephp.org/2.0/en/controllers/pages-controller.html
     */
    class UsersController extends AppController {

        var $name = "Users";

        public function beforeFilter() {

            parent::beforeFilter();
            $this->Auth->allow( "login", "register" );
        }

        public function index() {
            if( $this->Auth->user['tip'] == "profesor" ) {
                $this->redirect( SITE_URL . "profesor/afsare_teme" );
            }
            else {
                $this->redirect( SITE_URL . "students/index" );
            }
        }

        /**
         * Gestioneaza o incercare de login
         */
        public function login() {
            if( $this->request->is( "post" ) ) {
                if( $this->Auth->login() ) {
                    if( $this->Auth->user( "tip" ) == "profesor" ) {
                        $this->redirect( array( "controller" => "profesor", "action" => "profile" ) );
                    }
                    if( $this->Auth->user( "tip" ) == "student" ) {
                        $this->redirect( array( "controller" => "students", "action" => "index" ) );
                    }
                }
                else {
                    $this->Session->setFlash( "Username/password gresite", "flash_error" );
                }
            }
        }

        /**
         * Gestioneaza o incercare de inregistrare a unui nou user
         */
        public function register() {
            if( $this->request->is( "post" ) ) {
                if( $this->User->save( $this->request->data ) ) {
                    $this->Auth->login();
                    $this->Session->setFlash( "Utilizator creat", "flash_success" );
                    $this->index();
                }
                else {
                    $this->Session->setFlash( "Date invalide", "flash_success" );
                }
            }
        }

        /**
         * Gestioneaza delogarea
         */
        public function logout() {
            $this->Auth->logout();
            $this->Session->setFlash( "Logged out!", "flash_success" );
            $this->redirect( SITE_URL );
        }

        /*
        public function test_email() {
            $to = array();
            $to['popica93@gmail.com']='Popa Andrei';
            /*
            foreach($admin_users as $user)
                if ($user['User']['email']!='danielpopescu93@gmail.com')
                    {
                    $to[$user['User']['email']] = $user['User']['first_name'];
                    }
            $from = array(SMTP_MANDRILL_FROM =>SMTP_MANDRILL_FROM_NAME);
            $email = $this->Email->find('first',array('conditions'=>array('name'=>'test')));
            $subject = $email['Email']['subject'];
            
            
            $html = $email['Email']['text'];
            $html = str_replace('[var1]',"O variabila",$html);
            $html = str_replace('[var2]',"Alta variabila",$html);
            
            
            
            $transport = Swift_SmtpTransport::newInstance(SMTP_MANDRILL_URL, SMTP_MANDRILL_PORT);
            $transport->setUsername(SMTP_MANDRILL_USER);
            $transport->setPassword(SMTP_MANDRILL_PASS);
            $swift = Swift_Mailer::newInstance($transport);
            
            $message = new Swift_Message($subject);
            $message->setFrom($from);
            $message->setBody($html, 'text/html');
            $message->setTo($to);
                    
            $recipients = $swift->send($message, $failures);
        }*/
    }
